﻿//
// Created by Grishul Eugeny
//
// See license in License.txt
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace NCommander
{
	internal static class Extensions
	{
		public static T GetAttribute<T>( this Type type )
		{
			var result = type.GetCustomAttributes( typeof( T ), false );

			if( result.Length == 0 )
				throw new Exception( string.Format( "No such attribute: {0}", typeof( T ) ) );

			if( result.Length > 1 )
				throw new Exception( string.Format( "Too many results for attribute: {0}", typeof( T ) ) );

			return ( T ) result[0];
		}

		public static void DisableFlicker( this Control control )
		{
			control.GetType().GetMethod( "SetStyle", BindingFlags.Instance | BindingFlags.NonPublic ).Invoke( control, new object[] { ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true } );
		}
	}
}
